package com.konsung.ksdc.common.widget;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.konsung.ksdc.R;
import com.konsung.ksdc.common.utils.Constants;
import com.konsung.ksdc.common.utils.PreferenceUtils;

/**
 * 登录界面让测试人员选择域名的popwndow
 */
public class IpPopupWindow extends PopupWindow {
    private View view;
    protected EditText etIp; //服务器ip
    protected EditText etPort; //服务器端口
    Button btnCancel;
    Button btnConfirm;
    private Context context;
    private final LinearLayout llPop;

    /**
     * 构造方法
     * @param context 上下文
     * @param confirm 确认
     * @param cancel 取消
     */
    public IpPopupWindow(Context context, View.OnClickListener confirm,
            View.OnClickListener cancel) {

        super(context);
        this.context = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context
                .LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.ip_select_popup, null);
        llPop = (LinearLayout) view.findViewById(R.id.login_ll_pop);
        etIp = (EditText) view.findViewById(R.id.et_ip);
        etPort = (EditText) view.findViewById(R.id.et_port);
        btnCancel = (Button) view.findViewById(R.id.btn_cancel);
        btnConfirm = (Button) view.findViewById(R.id.btn_confirm);
        String popIp = PreferenceUtils.getString(context, Constants.LOGIN_POP_IP, Constants
                .LOGIN_POP_IP);
        //如果没有设置，默认显示
        if (!popIp.equals(Constants.LOGIN_POP_IP)) {
            etIp.setText(popIp);
        }
        String popPort = PreferenceUtils.getString(context, Constants.LOGIN_POP_PORT, Constants
                .LOGIN_POP_PORT);
        //如果没有设置，默认显示
        if (!popPort.equals(Constants.LOGIN_POP_PORT)) {
            etPort.setText(popPort);
        }
        btnConfirm.setOnClickListener(confirm);
        btnCancel.setOnClickListener(cancel);

        //设置PopupWindow的View
        this.setContentView(view);
        //设置PopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置PopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置PopupWindow弹出窗体可点击
        this.setFocusable(false);
        this.getBackground().setAlpha(100);
    }

    /**
     * 得到域名
     * @return ip
     */
    public String getIp() {
        String ip = etIp.getText().toString().trim();
        if (TextUtils.isEmpty(ip)) {
            ip = etIp.getHint().toString();
        }
        PreferenceUtils.putString(context, Constants.LOGIN_POP_IP, ip);
        return ip;
    }

    /**
     * 得到域名
     * @return port
     */
    public String getPort() {
        String port = etPort.getText().toString().trim();
        if (TextUtils.isEmpty(port)) {
            port = etPort.getHint().toString();
        }
        PreferenceUtils.putString(context, Constants.LOGIN_POP_PORT, port);
        return port;
    }
}
